package com.ssh.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ssh.model.Role;
import com.ssh.service.RoleService;


@Controller
@RequestMapping("/role")
public class RoleController {

	@Autowired
	private RoleService roleService;
	
	
	@RequestMapping("/toAddRole.do")
	public String toAddRole(){
		return "addRole";
	}
	
	@RequestMapping("/addRole.do")
	public String addRole(Role role){
		roleService.saveRole(role);
		return "redirect:roleView.do";
	}
	
	@RequestMapping("/getRoleById.do")
	public String getRoleById(Integer roleId , Model model){
		Role role = roleService.selectRole(roleId);
		model.addAttribute("role", role);
		return "updateRole";
	}
	
	@RequestMapping("/updateRole.do")
	public String updateRole(Role role){
		roleService.updateRole(role);
		return "redirect:roleView.do";
	}
	
	@RequestMapping("/deleteRole.do")
	public String deleteRole(Role role){
		roleService.deleteRole(role);
		return "redirect:roleView.do";
	}
	
	@RequestMapping("/roleView.do")
	public String roleView(Role role, Model model){
		
		List<Role> roles = roleService.selectAll(role);
		model.addAttribute("roles",	roles);
		return "roleShow";
	}
	
}
